//
//  IdentityCardTextField.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/7/27.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class IdentityCardTextField: BaseTextField {
    var numKeyBoard:JCKeyBoardNum?
    var isOpenIdentyKeyBoard:Bool = true // 默认开启
    override init(changeView: UIView?, szBounds: CGRect) {
        super.init(changeView: changeView, szBounds: szBounds)
        self.keyboardType = UIKeyboardType.asciiCapable
        self.delegate = self
        self.font = UIFont.systemFont(ofSize: 15)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
        if !isOpenIdentyKeyBoard {
            self.inputView = nil
            return true
        }
        numKeyBoard = JCKeyBoardNum.show()
        textField.inputView = numKeyBoard
        numKeyBoard?.completeBlock = {[weak self] (str:String?, length:Int) in
            if  length == 9 {
                if self?.text!.count != 0 && !(self?.text!.contains("X"))! &&
                    self?.text!.count == 17 {
                    self?.changText(with: "X")
                }
//                self?.viewModel?.idCard = textField.text!
            }else if length == 11 {
                self?.clickDeleteBtn(textField: textField)
//                self?.viewModel?.idCard =  textField.text!
            }else {
                textField.changText(with: str)
//                self?.viewModel?.idCard =  textField.text!
            }
            
            if (textField.text?.ch_length)! > 18 {
                textField.text = (textField.text! as NSString).substring(to: 18)
            }
            if self?.textDelegate != nil && self?.textDelegate?.textFieldDidChange != nil {
                self?.textDelegate?.textFieldDidChange!(textField: self!)
            }
        }
        return true
    }
    func clickDeleteBtn(textField:UITextField) {
//        if (textField.text?.ch_length)! > 0 {
//            textField.text = (textField.text! as NSString).substring(to: (textField.text?.ch_length)! - 1)
//        }
        let range = textField.selectedTextRange
        let location = textField.offset(from: textField.beginningOfDocument, to: (range?.start)!)
        if location == 0 {
            return
        }
        if (textField.text?.ch_length)! >= location {
            textField.text = (textField.text! as NSString).replacingCharacters(in: NSRange(location: location-1, length: 1), with: "")
            let newPods = textField.position(from: textField.beginningOfDocument, offset: location-1)
            if newPods == nil {
                return
            }
            textField.selectedTextRange = textField.textRange(from: newPods!, to: newPods!)
        }
    }
    
    override func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if !super.textField(textField, shouldChangeCharactersIn: range, replacementString: string) {
            return false
        }
        
        if string.count == 0 {
            return true
        }
        
        let newString = (self.text! as NSString).replacingCharacters(in: range, with: string)
        if isOpenIdentyKeyBoard {
            if newString.count > 18 {
                return false
            }
        }else {
            if newString.count > 20 {
                return false
            }
        }
        return textField.judgePassRule(pass: string)
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
